/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Review.test2;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author THIEN
 */
public class StudentManagement {

    public StudentManagement() {
    }
    Student student;
    ArrayList<Student> StudentList = new ArrayList<Student>();
    InputStreamReader reader = new InputStreamReader(System.in);
    BufferedReader in = new BufferedReader(reader);

    public void inputData() throws IOException {
        for (int i = 0; i < 2; i++) {
            System.out.println("Input roll number: ");
            String rollNumber = in.readLine();

            System.out.println("Input name: ");
            String name = in.readLine();

            System.out.println("Input age: ");
            int age = Integer.parseInt(in.readLine());

            student = new Student(rollNumber, name, age);
            StudentList.add(student);
        }
        for (Student student : StudentList) {
            System.out.println(student);

        }
    }

    public void save2file() throws FileNotFoundException, IOException {
        FileOutputStream f = new FileOutputStream("list.txt", true);
        ObjectOutputStream writer = new ObjectOutputStream(f);
        writer.writeObject(StudentList);
        writer.flush();
        writer.close();
        f.close();
    }

    public void readFromFile() throws FileNotFoundException {
        FileReader fReader = new FileReader("list.txt");
        BufferedReader bReader = new BufferedReader(fReader);
        ArrayList<Student> list = (ArrayList<Student>) StudentList;
        for (Student student : list) {
            System.out.println(student);

        }
    }

    public void searchData() throws IOException {
        System.out.println("Input name want to search");
        String search = in.readLine();
        Pattern pattObj = Pattern.compile(search, Pattern.CASE_INSENSITIVE);
        for (Student student : StudentList) {
            String temp = student.getName();
            Matcher matObj = pattObj.matcher(temp);
            while(matObj.find()){
                System.out.println("Found");
                System.out.println(student);
            }

        }

    }
}
